//package com.zmn.plat.manager.config;
//
//import com.alibaba.fastjson.parser.Feature;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.alibaba.fastjson.support.config.FastJsonConfig;
//import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
//import org.springframework.boot.autoconfigure.cache.CacheProperties;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
///**
// * @author quanjic
// * @version v1.0
// * @since 2018/11/15 10:24
// **/
//@Configuration
//@ConfigurationProperties(prefix = "zmn.cache")
//class ZmnRedisCacheConfiguration {
//
//    private Map<String, CacheProperties.Redis> redis = new ConcurrentHashMap<>();
//
//    private boolean allowInFlightCacheCreation = false;
//
//    private String keyPrefix;
//    private boolean userKeyPrefix = true;
//
//    @Bean("zmnRedisCacheManager")
//    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
//        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
//                .builder(redisConnectionFactory);
//        if (!redis.isEmpty()) {
//            Map<String, RedisCacheConfiguration> maps = new ConcurrentHashMap<>();
//            redis.forEach((key, value) -> maps.put(key, determineConfiguration(value, resourceLoader.getClassLoader())));
//            builder = builder.withInitialCacheConfigurations(maps);
//        }
//        if (!allowInFlightCacheCreation) {
//            builder = builder.disableCreateOnMissingCache();
//        }
//        return builder.build();
//    }
//
//    private FastJsonRedisSerializer getFastJsonRedisSerializer() {
//
//        FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class);
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteClassName);
//        fastJsonConfig.setFeatures(Feature.SupportAutoType);
//
//        serializer.setFastJsonConfig(fastJsonConfig);
//
//        return serializer;
//    }
//
//    private JdkSerializationRedisSerializer getJdkSerializationRedisSerializer(ClassLoader classLoader) {
//        return new JdkSerializationRedisSerializer(classLoader);
//    }
//
//    private RedisCacheConfiguration determineConfiguration(CacheProperties.Redis redisProperties,
//                                                           ClassLoader classLoader) {
//        RedisCacheConfiguration config = RedisCacheConfiguration
//                .defaultCacheConfig();
//
//        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair
//                .fromSerializer(getFastJsonRedisSerializer()));
//
//
//        if (redisProperties.getTimeToLive() != null) {
//            config = config.entryTtl(redisProperties.getTimeToLive());
//        }
//        if (redisProperties.getKeyPrefix() != null) {
//            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
//        } else if (keyPrefix != null) {
//            config = config.prefixKeysWith(keyPrefix);
//        }
//        if (!redisProperties.isCacheNullValues()) {
//            config = config.disableCachingNullValues();
//        }
//        if (!redisProperties.isUseKeyPrefix() && !userKeyPrefix) {
//            config = config.disableKeyPrefix();
//        }
//        return config;
//    }
//
//    public Map<String, CacheProperties.Redis> getRedis() {
//        return redis;
//    }
//
//    public void setRedis(Map<String, CacheProperties.Redis> redis) {
//        this.redis = redis;
//    }
//
//    public boolean isAllowInFlightCacheCreation() {
//        return allowInFlightCacheCreation;
//    }
//
//    public void setAllowInFlightCacheCreation(boolean allowInFlightCacheCreation) {
//        this.allowInFlightCacheCreation = allowInFlightCacheCreation;
//    }
//
//    public String getKeyPrefix() {
//        return keyPrefix;
//    }
//
//    public void setKeyPrefix(String keyPrefix) {
//        this.keyPrefix = keyPrefix;
//    }
//
//    public boolean isUserKeyPrefix() {
//        return userKeyPrefix;
//    }
//
//    public void setUserKeyPrefix(boolean userKeyPrefix) {
//        this.userKeyPrefix = userKeyPrefix;
//    }
//}
